import { useLazyQuery } from "@apollo/client";
import {
  getCardRecords_service,
  getNormalCardRecordsByCourse_service,
} from "../../graphql/cardRecord";
import { CardRecordType } from "../../types/cardRecord";

export const useFetch_getCardRecords = () => {
  const [fun, { loading }] = useLazyQuery(getCardRecords_service, {
    fetchPolicy: "network-only",
  });

  const getCardRecords = async (pageNum: number, pageSize: number) => {
    const res = await fun({
      variables: {
        page: {
          pageNum,
          pageSize,
        },
      },
    });
    return res.data.getCardRecords.data as CardRecordType[];
  };

  return {
    loading,
    getCardRecords,
  };
};

export const useGetNormalCardRecordsByCourse = () => {
  const [fun, { loading }] = useLazyQuery(
    getNormalCardRecordsByCourse_service,
    {
      fetchPolicy: "network-only",
    }
  );

  const getNormalCardRecordsByCourse = async (courseId: string) => {
    const res = await fun({
      variables: {
        courseId,
      },
    });
    return {
      data: res.data.getNormalCardRecordsByCourse.data as CardRecordType[],
      code: res.data.getNormalCardRecordsByCourse.code as boolean,
    };
  };

  return {
    loading,
    getNormalCardRecordsByCourse,
  };
};
